package com.juyu.miliao.common.util;


import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juyu.miliao.common.constant.EnumType;
import com.juyu.miliao.common.constant.SpendSystemMessage;
import com.juyu.miliao.common.netease.NetEaseCloudUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * 注册项目中的 发送IM 消息
 * @Auther: 卜加超
 * @Date: 2019/3/26 11:09
 * @Description:
 */

public class MessageSystemUtil {
    private static final Logger logger = LoggerFactory.getLogger(MessageSystemUtil.class);

    /**
     *
     * 注册成功女用户发送小助手通知
     * @param userId
     */
    public static void userCertificationHost(long userId ,String message) {

       JSONObject body = new JSONObject();
        body.put("msg", message);
        //开发者扩展字段
        JSONObject ext = new JSONObject();
        ext.put("buttonType",EnumType.BUTTON_TYPE.state); // 按钮属性 1：去认证
        try {
            String temp = NetEaseCloudUserService.getSendMsgByRegister(userId,EnumType.HELPER_USER_ID.state,
                    EnumType.TEXT_MESSAGE.state, body.toString(), ext.toString(), "");
            logger.info("手机注册成功女用户发送消息 userId={},body={},ext{}",userId,body.toString(),ext);
            if (!temp.substring(temp.indexOf("code") + 6, temp.indexOf("code") + 9).equals("200")) {
                logger.error("发送点对点普通消息异常-云信：userId={},message={}", userId, temp);
            }

        } catch (Exception e) {
            logger.error("发送点对点普通消息异常：userId={},error={}", userId, e);
        }
    }

    /**
     * 首次注册发送温馨提示
     * @param userId
     */
    public static void sendRegisterMessage(long userId){
        try {
            String message = SpendSystemMessage.FIRST_REGISTER_SEND_MESSAGE.getMessage();
            Map<String,String> map=new HashMap<String,String>();
            map.put("title", "寻寻");
            map.put("type", "1");
            map.put("message", message);
            ObjectMapper mapper = new ObjectMapper();
            String createLogInfo= NetEaseCloudUserService.getSystemMessage(mapper.writeValueAsString(map), userId);
            logger.info("sendRegisterMessage系统消息发送成功：message={},userid={}",map.get("message"),userId);
            if(!createLogInfo.substring(createLogInfo.indexOf("code")+6, createLogInfo.indexOf("code")+9).equals("200")){
                if(logger.isErrorEnabled()){
                    logger.error("sendRegisterMessage系统消息发送IM失败：系统错误信息" + createLogInfo + "\n" + "参数：" + userId);
                }
            }
        } catch (Exception e) {
            logger.error("sendRegisterMessage系统消息发送IM错误：参数-" + userId,e);
        }
    }


    /**
     * 发送自定义系统消息
     * @param userId
     */
    public static void sendCustomSystemMessage(long userId,String message){
        try {
            Map<String,String> map=new HashMap<String,String>();
            map.put("title", "寻寻");
            map.put("type", "1");
            map.put("message", message);
            ObjectMapper mapper = new ObjectMapper();
            String createLogInfo= NetEaseCloudUserService.getSystemMessage(mapper.writeValueAsString(map), userId);
            logger.info("sendCustomSystemMessage系统消息：return={},userId={}",createLogInfo,userId);
            if(!createLogInfo.substring(createLogInfo.indexOf("code")+6, createLogInfo.indexOf("code")+9).equals("200")){
                if(logger.isErrorEnabled()){
                    logger.error("sendRegisterMessage系统消息发送IM失败：系统错误信息" + createLogInfo + "\n" + "参数：" + userId);
                }
            }
        } catch (Exception e) {
            logger.error("sendRegisterMessage系统消息发送IM错误：参数-" + userId,e);
        }
    }
}
